package com.geor.gcr.idc;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import org.nutz.lang.Files;

import java.io.File;

/**
 * Created by Administrator on 2017/11/27.
 */

public class ConfigurationActivity extends AppCompatActivity {
    File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), "tmp/params.txt");

    @Override
    protected void onStart() {
        super.onStart();
        try {
            if (!file.exists()) {
                file.mkdir();
//                Files.write(file, Constants.address + "," + Constants.port);
            } else {
//                String param = Files.read(file);
//                if (param == null) {
//                    return;
//                }
//                String params[] = param.trim().split(",");
//                if (params.length == 2) {
//                    Constants.address = params[0];
//                    Constants.port = Integer.parseInt(params[1]);
//                    EditText addressText = (EditText) findViewById(R.id.address);
//                    EditText portText = (EditText) findViewById(R.id.port);
//                    addressText.setText(Constants.address);
//                    portText.setText(String.valueOf(Constants.port));
//                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_configuration);
    }

    int WRITE_EXTERNAL_STORAGE_REQUEST_CODE = 0;

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        doNext(requestCode,grantResults);
    }

    private void doNext(int requestCode, int[] grantResults) {
        if (requestCode == WRITE_EXTERNAL_STORAGE_REQUEST_CODE) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                System.out.println("允许!@#");
            } else {
                System.out.println("不允许!@#");
            }
        }
    }

    public void setConfig(View view) {

        if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {
            //申请WRITE_EXTERNAL_STORAGE权限
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                    WRITE_EXTERNAL_STORAGE_REQUEST_CODE);
        }

        EditText addressText = (EditText) findViewById(R.id.address);
        EditText portText = (EditText) findViewById(R.id.port);
        try {
            String address = addressText.getText().toString().trim();
            int port = Integer.parseInt(portText.getText().toString().trim());
            if(port < 0  || port > 99999) {
                throw new Exception();
            }
            Constants.address = address;
            Constants.port = port;
//            Intent intent = new Intent(this, MyCameraActivity.class);
            Intent intent = new Intent(this, MainActivity.class);
            startActivity(intent);
        }catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(this, "地址或端口格式错误", Toast.LENGTH_SHORT).show();
        }

        System.out.println(Constants.address + "," + Constants.port + "!@#");
//        Files.write(file, Constants.address + "," + Constants.port);
    }

}
